/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Engine;


import Control.GameController;
import Objects.MovableObject;
import Players.HumanPlayer;
import Ships.Ship;

/**
 * Classe respons?vel por executar as a??es das teclas e que tem a lista das flags de cada tecla.
 * @author Gustavo Buzogany Eboli
 */
public class KeyActions extends KeyControl {

    private Engine engine;
    private HumanPlayer playerLink;
    private static KeyActions myInstance;

    /**
     * Construtor da classe, privado por ser singleton
     */

    public void pool()
    {
        if(keyMap['p'] == 1)
        {
            GameController.getInstance().switchPaused();
            keyMap['p'] = 3;
        }
        else if (keyMap['p'] == 2)
        {
            keyMap['p'] = 0;
        }
        if(keyMap['w'] == 1)
        {
            playerLink.accelerate(1.0f);
        }
        else if (keyMap['w'] == 2)
        {
            playerLink.clearAccelerate();
            keyMap['w'] = 0;
        }
        if(keyMap['s'] == 1)
        {
            playerLink.accelerate(-1.0f);
        }
        else if (keyMap['s'] == 2)
        {
            playerLink.clearAccelerate();
            keyMap['s'] = 0;
        }
        if(keyMap['a'] == 1)
        {
            playerLink.accelerateAng(1.0f);
        }
        else if (keyMap['a'] == 2)
        {
            playerLink.clearAccelerateAng();
            keyMap['a'] = 0;
        }
        if(keyMap['d'] == 1)
        {
            playerLink.accelerateAng(-1.0f);
        }
        else if (keyMap['d'] == 2)
        {
            playerLink.clearAccelerateAng();
            keyMap['d'] = 0;
        }
        if (keyMap[' '] == 1) {
            Ship player = engine.getHumanPlayer().getMyShip();
            player.shootLaser();
        }
        else if (keyMap[' '] == 2)
        {
            keyMap[' '] = 0;
        }

        if (keyMap['x'] == 1) {
            Ship player = engine.getHumanPlayer().getMyShip();
            if(engine.getSelected().size() > 0) {
                Integer auxKey = (Integer) engine.getSelected().lastKey();
                MovableObject temp = (MovableObject) engine.getSelected().get(auxKey);
                player.shootMissile(temp);
            }
            keyMap['x'] = 2;
        }
        else if (keyMap['x'] == 2)
        {

            keyMap['x'] = 0;
        }
    }

    public void setEngine(Engine engine)
    {
        this.engine = engine;
        playerLink = engine.getHumanPlayer();
    }

    /**
     * M?todo respons?vel por retornar a ?nica inst?ncia da classe (singleton)
     * @return Retorna a ?nica inst?ncia da classe.
     */
    public static KeyActions getInstance()
    {
        if(myInstance == null)
        {
            myInstance = new KeyActions();
        }
        return myInstance;
    }
}